package view;


import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;

/**
 * This class manages the token view
 * 
 * The token view will allow a user to add, view or delete tokens.
 * 
 * @author MUSAB
 * @since May 2013
 * @version $Revision: 1.0 $
 */
public class TokenView{
	
	/**
	 * Models for the system and user defined tokens
	 */
	DefaultTableModel userTokens;
	
	/**
	 * JTables to display the system and user tokens
	 */
	JTable userTokensTable;
	
	/**
	 * Buttons
	 */
	JButton addNewTokenBtn, viewSystemTokensBtn, deleteSelectedBtn, usageBtn;
	
	/**
	 * Scroll pane for tokens table
	 */
	JScrollPane scrollPane;
	
	/**
	 * Text fields
	 */
	JTextField newKey, newValue;
	
	/**
	 * Constructor, sets up the window
	 */
	public TokenView(){
		
		initializeComponents();
	}

	/**
	 * Initializes the components in this view
	 */
	private void initializeComponents() {

		addNewTokenBtn = new JButton(IconsManager.getImageIcon("small/plus.png"));
		
		deleteSelectedBtn = new JButton(IconsManager.getImageIcon("small/cross.png"));
		
		usageBtn = new JButton("Usage", IconsManager.getImageIcon("small/question.png"));		
		viewSystemTokensBtn = new JButton("System tokens", IconsManager.getImageIcon("small/information.png"));
		
		newKey = new JTextField();
		newValue = new JTextField();
		
		userTokens = new DefaultTableModel(){
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		userTokens.setColumnIdentifiers( new String[] {"Key","Value"});
		userTokensTable = new JTable(userTokens);
		userTokensTable.getTableHeader().setReorderingAllowed(false);
		userTokensTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		userTokensTable.setOpaque(false);
		scrollPane = new JScrollPane(userTokensTable);

	}

	/**
	 * Creates a joption pane with the components and shows it
	 */
	public void showDialog() {
		
		// Create a container panel
		JPanel mainContainer = new JPanel(new MigLayout("fillx, width 400"));
		
		// Adding components to the panel
		mainContainer.add(scrollPane, "wrap, growx, height 50::100");
		mainContainer.add(new JLabel("Key:"), "split");
		mainContainer.add(newKey, "growx, split");
		mainContainer.add(new JLabel("Value:"), "split");
		mainContainer.add(newValue, "growx, split");
		mainContainer.add(addNewTokenBtn, "split");
		mainContainer.add(deleteSelectedBtn, "wrap");
		mainContainer.add(viewSystemTokensBtn, "al right, split");
		mainContainer.add(usageBtn, "al right, wrap");
		
		
		// Show as dialog
		JOptionPane.showOptionDialog(null, mainContainer, "Manage tokens",
				JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
	}


	/**
	
	 * @return the userTokensTable */
	public JTable getUserTokensTable() {
		return userTokensTable;
	}

	/**
	
	 * @return the viewSystemTokensBtn */
	public JButton getViewSystemTokensBtn() {
		return viewSystemTokensBtn;
	}
	
	/**
	
	 * @return the usageBtn */
	public JButton getUsageBtn() {
		return usageBtn;
	}
	
	/**
	
	 * @return the userTokens */
	public DefaultTableModel getUserTokens() {
		return userTokens;
	}

	/**
	
	 * @return the addNewTokenBtn */
	public JButton getAddNewTokenBtn() {
		return addNewTokenBtn;
	}
	
	/**
	
	 * @return the deleteSelectedBtn */
	public JButton getDeleteSelectedBtn() {
		return deleteSelectedBtn;
	}



	/**
	 * @param userTokens the userTokens to set
	 */
	public void setUserTokens(DefaultTableModel userTokens) {
		this.userTokens = userTokens;
	}

	
	/**
	 * @param userTokensTable the userTokensTable to set
	 */
	public void setUserTokensTable(JTable userTokensTable) {
		this.userTokensTable = userTokensTable;
	}

	/**
	 * @param addNewTokenBtn the addNewTokenBtn to set
	 */
	public void setAddNewTokenBtn(JButton addNewTokenBtn) {
		this.addNewTokenBtn = addNewTokenBtn;
	}

	/**
	
	 * @return the newKey */
	public JTextField getNewKey() {
		return newKey;
	}
	
	/**
	
	 * @return the newValue */
	public JTextField getNewValue() {
		return newValue;
	}

	/**
	 * @param newKey the newKey to set
	 */
	public void setNewKey(JTextField newKey) {
		this.newKey = newKey;
	}

	/**
	 * @param newValue the newValue to set
	 */
	public void setNewValue(JTextField newValue) {
		this.newValue = newValue;
	}
}
